<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车全选效果</title>
</head>
<body>
    <table cellspacing="0" cellpadding="10px" align="center">
        <thead>
            <tr>
                <th>
                    <input type="checkbox" name="" id="selectAll">
                </th>
                <th>商品名称</th>
                <th>商品价格</th>
            </tr> 
        </thead>
        <tbody id="selectOne">
            <tr>
                <td>
                    <input type="checkbox" name="" >
                </td>
                <td>棒棒糖</td>
                <td>1.0</td>
            </tr> 
            <tr>
                <td>
                    <input type="checkbox" name="" >
                </td>
                <td>辣条</td>
                <td>3.0</td>
            </tr> 
            <tr>
                <td>
                    <input type="checkbox" name="" >
                </td>
                <td>汽水</td>
                <td>4.0</td>
            </tr> 
            <tr>
                <td>
                    <input type="checkbox" name="" >
                </td>
                <td>口香糖</td>
                <td>5.0</td>
            </tr> 
        </tbody>
    </table>    
</body>
<script>
    //全选
    let selectAll = document.querySelector("#selectAll")
    //所有单选
    let inputs = document.querySelector("#selectOne").getElementsByTagName("input")
    
    //1.给全选框绑定点击事件，效果：将inputs内所有元素标签的选中状态设置 与 全选框一致
    selectAll.onclick = function(){
        for (const element of inputs) {
           element.checked = this.checked
        }
    }

    //2.给每个单选框绑定点击事件，效果：如果单选框全部选中，则全选框选中，否则全选框不选中
    for (const element of inputs) {
        element.onclick = function(){
            //检查是否选中每个单选框
            let flag = true
            for (const e of inputs) {
                if(!e.checked){
                    flag = false
                    break
                }
            }
            //设置全选框的选中状态
            selectAll.checked = flag
        }
    }

</script>
</html>